package recommender_online;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import recommender_data_process.Recommendar_data_process;



public class Recommender_online {

	public static void main(String[] args) throws ClassNotFoundException, IOException {
		Recommendar_data_process test = new Recommendar_data_process();
		test.readFile("ratings_train_test.txt");
		test.item_to_userMap();
		test.user_to_itemMap();
		test.checkNeighbour();
		test.similarityCalculate();
		test.readSimTable();
		Map map = test.getMapCache();
		RecommenderOnlineDetailImplements tt = new RecommenderOnlineDetailImplements();
		tt.recommenderOnline(map);
	}

}
class RecommenderOnlineDetailImplements{
	
	public void recommenderOnline(Map map){
		Scanner input  = new Scanner(System.in);
		System.out.println("\nInputed Movie:");
		int movieID = input.nextInt();
		Iterator it = map.entrySet().iterator();
		Map.Entry entry;
		for(int i = 0; i < map.size(); i++){
			entry = (Map.Entry) it.next();
			int intKey = (Integer)entry.getKey();
	        if(movieID == intKey){
	        	ArrayList value = (ArrayList)entry.getValue();
	        	String[][] result = new String[value.size()][2];
	        	for(int j = 0; j < value.size(); j++){
	        		String[] temp = new String[2];
		        	temp = (String[])value.get(j);
	        		 result[j][0] = temp[0];
	        		 result[j][1] = temp[1];
	        	}
	        	for(int m = 0; m < result.length - 1; m++){
	        		int k = m;
	        		for(int n = m + 1; n < result.length; n++){
	        			if(Float.parseFloat(result[n][1]) > Float.parseFloat(result[k][1]))
	        				k = n;
	        		}
	        		if(k != m){
	        			String t1 ,t2;
	        			t1 = result[k][0];
	        			t2 = result[k][1];
	        			result[k][0] = result[m][0];
	        			result[k][1] = result[m][1];
	        			result[m][0] = t1;
	        			result[m][1] = t2;
	        		}
	        	}
	        	System.out.println("Recommender movies:");
	        	for(int p = 0; p < result.length; p++){
	        		if(p == 14)
	        			break;
	        		System.out.println(result[p][0] + "   " + result[p][1]);
	        	}
	        }  
		}
	}
}


